package com.cn.kabalifeuser.ui.integral.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.integral.adapter.ShoppingMallAdapter;
import com.cn.kabalifeuser.ui.integral.bean.BannerActBean;
import com.cn.kabalifeuser.ui.integral.bean.ShoppingMalListBean;
import com.cn.kabalifeuser.utils.GlideImageLoader;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.NotDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 晓林 on 2020/9/14.
 * function: 活动专区
 */
public class ActSpecialAreaActivity extends IBaseActivity<ActSpecialAreaView, ActSpecialAreaPresenter> implements ActSpecialAreaView {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    Bundle bundle;

    BannerActBean.ResultBean.DataBean data;
    List<ShoppingMalListBean.ResultBean.DataBean> mList;
    ShoppingMallAdapter mAdapter;
    private View notDataView;

    @Override
    protected ActSpecialAreaPresenter createPresenter() {
        return new ActSpecialAreaPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_act;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        bundle = getIntent().getExtras();
        data = (BannerActBean.ResultBean.DataBean) bundle.getSerializable("data");
        setTitleBar(data.getTitle());
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(data.getImg());
        //动画效果
        //banner.setBannerAnimation(Transformer.Accordion);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        mList = new ArrayList<>();
        rvData.setLayoutManager(new GridLayoutManager(getMe(), 2));
        rvData.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ShoppingMallAdapter(R.layout.fragment_mall_item, mList, 2);
        //给RecyclerView设置适配器
        rvData.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(this, rvData, refreshLayout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo 跳转详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                $startActivity(IntegralCommodityDetailsActivity.class, bundle);
            }
        });
        getListData();
    }

    @Override
    public void initRefreshLayout() {
        super.initRefreshLayout();
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onShoppingMallData(LoginHelper.getLonginData().getResult(), data.getId(), pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onShoppingMalListSuccess(ShoppingMalListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }
}
